GBML manual / Language

Writing a type out, instead of in a comment

Every job a /// tag does - the type of a variable, how big a list or string is, what a script takes and gives back - can also be written as normal code; this page lists both ways side by side.

Two ways to say the same thing

Some comments in GBML are not just notes to yourself. A line that starts with /// and then an @ word - /// @capacity 16, say - is a tag, and the compiler reads it. It changes what the program is. Most comments are ignored, so a comment that changes the game can be a surprise.

So every tag has a second way to say the same thing, written as normal code with no comment involved. Both ways are fully supported, and neither one is going away. The tags are not deprecated. A project can use one, the other, or a mix, even on the same line of code. Use whichever reads better to you.

Every tag, and what else says it

This is the whole list. The left column is the way you may already know. The right column is the new way.

The jobThe tag (still works)Written as codeWhat kind of thing it is
Say what type a variable is/// @type {T} above the linevar name: T = value; (or name: T = value; for an instance variable)a colon and a type after the name
Say how many a list, array, stack, queue, set or map can hold/// @capacity Nlist<int, N>, array<int, N>, stack<int, N>, queue<int, N>, set<int, N>, map<int, int, N> - the size goes lasta number inside the type
Say how big a grid is/// @capacity WxHgrid<int, W, H>two numbers inside the type
Say how many characters a string can hold/// @capacity Nstring<N>a number inside the type
Say what type a script's parameter is/// @arg {T} namefunction f(name: T)a colon and a type after the parameter
Say what type a script gives back/// @returns {T}function f(): Ta colon and a type after the parentheses
Make an array of a set size with every slot starting the same/// @type {array<T>} + /// @capacity N + a list of N matching valuesarray_create(N, fill)a function
Make a grid of a set size with every cell starting the same/// @type {grid<T>} + /// @capacity WxH (the cells start at 0)grid_create(W, H, fill)a function
Make an empty list, stack, queue or set that holds up to N/// @type {list<T>} + /// @capacity N + []list_create(N, example), stack_create(N, example), queue_create(N, example), set_create(N, example)a function
Make an empty map that holds up to N pairs/// @type {map<K,V>} + /// @capacity N + {}map_create(N, key_example, value_example)a function

The functions that make a container

Seven of them, one for each kind of container. They all do one job: say what the container holds and how big it is, in one line of normal code.

FunctionWhat it makesWhich tags it can stand in for
array_create(size: int, fill: element) -> array<T>an array with size slots, every one starting as fill; the array's type follows from fill/// @type {array<T>} and /// @capacity size, plus writing the starting values out by hand
grid_create(width: int, height: int, fill: element) -> grid<T>a grid width across and height down, every cell starting as fill; the grid's type follows from fill/// @type {grid<T>} and /// @capacity WxH
list_create(capacity: int, example: element) -> list<T>an empty list that holds up to capacity/// @type {list<T>} and /// @capacity capacity
stack_create(capacity: int, example: element) -> stack<T>an empty stack that holds up to capacity/// @type {stack<T>} and /// @capacity capacity
queue_create(capacity: int, example: element) -> queue<T>an empty queue that holds up to capacity (a power of two)/// @type {queue<T>} and /// @capacity capacity
set_create(capacity: int, example: element) -> set<T>an empty set that holds up to capacity (a power of two)/// @type {set<T>} and /// @capacity capacity
map_create(capacity: int, key_example: element, value_example: element) -> map<K,V>an empty map that holds up to capacity pairs (a power of two)/// @type {map<K,V>} and /// @capacity capacity

The other rows of the first table are not functions. They are a small addition to how you write a type, so they add no function names to remember.

Two kinds of "second value"

The first two are different from the last five, and the difference is the whole reason there are two names for it: fill and example.

An example is any value of the right kind: 0 for whole numbers, 0.0 for decimals, "" for text, true for yes-or-no, { x: 0, y: 0 } for a struct. It is never run and never stored.

What it looks like

An instance variable (declared in the Create event) written both ways:

// the tag way
/// @type {list<int>}
/// @capacity 16
inventory = [];

// the written-out way - exactly the same variable
bag: list<int, 16> = [];

name: string<64> = "";
board: grid<int, 8, 6> = [];
scores = array_create(5, 0);
names = list_create(8, "");

And a variable inside an event, plus a script's signature:

var seen: list<int, 8> = [];
var flags = array_create(200, false);
var map_cells = grid_create(8, 6, -1);
var order = queue_create(8, 0);
// scripts/heal.gbml - the same script, both ways.

/// @arg {int} amount
/// @arg {list<int>} log
/// @returns {real}
function heal(amount, log) { ... }

function heal(amount: int, log: list<int>): real { ... }

Why a size is always a number you type

A list<int, 16> or array_create(16, 0) needs a number you can see in the code - 16, not a variable like n. This is not a rule GBML invented. It comes from the machine.

The Game Boy Advance has no way to hand out memory while a game runs. A container's size is fixed when the game is built, and stored in the program itself (it is part of the container's type in the C++ the compiler writes). A variable such as n does not have a value until the game is running, which is far too late. So the compiler needs the number right there in your code.

That is exactly what a /// @capacity tag has always needed, too. The only thing that was ever optional was writing it inside a comment. If you give a variable there instead, the compiler says so and explains why:

`array_create` needs its size to be a whole number written right there in the code,
but this is the variable `n`.

What the sizes cannot do

Which one to use

All of these work, and you can mix them. Some guidance for choosing:

All of them are the same size, the same speed and the same list in the finished game.

Why a list takes an example and an array takes a fill

A list starts empty. So when you write list_create(16, 0), the 0 cannot be something that goes in the list, because nothing goes in. It is only there to answer one question - "a list of what?" - and 0 answers "whole numbers." An array has no such problem: all its slots exist, so its second value is the thing every slot holds, and it names the kind as a side effect.

If you would rather not hand over a value that is never used, write the type instead: var inventory: list<string, 16> = []; says the same thing with nothing made up.

Where each way is explained in full

See also: Variables, types and scopeArraysGridsarray_creategrid_createlist_createstack_createqueue_createset_createmap_createScript parametersData containers